import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Karthik T
 */

public final class ball implements Runnable {
    int posX,posY,defaultX,defaultY;
    int velX, velY,speed=10, speedmax = 30;
    boolean matchOver=true;
    boolean topWall=true;
    public boolean waiting=false;

    private boolean CheckExit = false; // Check for the Exit command from player

    private Random random;
    
    //private final int initSpeed=10;
    //private int speed=initSpeed;
    
    private Thread ballThread;

    public Sprite ballSprite;

    public ball(int screen_width,int screen_height,int num_players,Image ballImage){
        ballSprite = new Sprite(ballImage);
        defaultX=screen_width/2;
        defaultY=screen_height/2;
        ballThread=new Thread(this);
        if(num_players==2)
            topWall=false;
        random = new Random();
    }

    public void start(){
        ballThread.start();
    }

    public void speedup(){
        //if(speed>3)speed--;
        if (speed<speedmax)
        speed+=2;
        System.out.println("Speed - "+speed+" Ball Vel - "+velX*speed/10+","+velY*speed/10);
    }

    public void run(){
        while(!CheckExit){
            if(matchOver){
                if(MainGame.mode==MainGame.JOIN&&waiting)continue;
                if(MainGame.mode!=MainGame.JOIN){
                    reset();
                    try{
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException ie){
                        ie.printStackTrace();
                    }
                    if(MainGame.mode==MainGame.HOST)MobilePong.connectMan.sendMatchStart();
                }
                matchOver=false;
            }
            try {
                move();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            try{
                Thread.sleep(15);
            }
            catch(InterruptedException ie){
                ie.printStackTrace();
            }
            //display();
        }

        System.err.println("ball::run() end");
    }

    public void move() throws InterruptedException{
        posX+=velX*speed/10;
        posY+=velY*speed/10;
        //ballSprite.setRefPixelPosition(posX, posY);
        ballSprite.setPosition(posX, posY);
        
        //Remove as more paddles are added, or add a dynamic way to know abt walls
        if(posX<0)
            collide(MainGame.LEFT);
        if(posX>defaultX*2-ballSprite.getWidth())
            collide(MainGame.RIGHT);
        if(posY<0){
            if(topWall) collide(MainGame.TOP);
            else{
                MainGame.local_paddle.incScore();
                matchOver();
            }
	  }
        if(posY>defaultY*2){
            MainGame.top_paddle.incScore();
            matchOver();
	  }
    }

    public void setPosX(int posX) {
        this.posX = posX;
    }

    public void setPosY(int posY) {
        this.posY = posY;
    }

    public void setVelX(int velX) {
        this.velX = velX;
    }

    public void setVelY(int velY) {
        this.velY = velY;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
    

    public void reset(){
        if (!CheckExit){
            posX=defaultX;
            posY=defaultY;
            do{
                velX=(random.nextInt(5)-2);
                velY=(random.nextInt(5)-2);
            }while(velX==0||velY==0||velX*velY==1||velX*velY==-1);
            ballSprite.setPosition(posX, posY);
            //speed=initSpeed;
            speed=10;
            System.out.println("Ball Vel - "+velX+","+velY);
            if(MainGame.mode==MainGame.HOST)MobilePong.connectMan.sendBallDet();
        }
    }

    public int getVelX(){
        return velX;
    }

    public int getVelY(){
        return velY;
    }

    public int getPosX(){
        return posX;
    }

    public int getPosY(){
        return posY;
    }

    public void collide(int Side){
        //System.out.println("COLLISION");
        switch(Side){
            case MainGame.LEFT: if(velX<0)velX=-velX;break;
            case MainGame.RIGHT:if(velX>0)velX=-velX;break;
            case MainGame.BOTTOM:{
                if(velY>0){
                    //velY=-velY;
                    MainGame.local_paddle.handleCollide(this);
                    speedup();
                    audio.paddle();
                }
                break;
            }
            case MainGame.TOP:{
                if(velY<0){
                    //velY=-velY;
                    MainGame.top_paddle.handleCollide(this);
                    if(!topWall){
                        speedup();
                        audio.paddle();
                    }
                }
                break;
            }
        }
        //if(MainGame.mode==MainGame.HOST)
        if(MainGame.BOTTOM==Side&&MainGame.mode!=MainGame.SINGLE){
            MobilePong.connectMan.sendPaddleDet();
            MobilePong.connectMan.sendBallCollide();
        }
    }

    public void matchOver(){
        audio.lost();
        if(MainGame.mode==MainGame.JOIN)waiting=true;
        if(MainGame.mode==MainGame.HOST)MobilePong.connectMan.sendMatchEnd();
        matchOver=true;
    }
    
    public void stop(){
        CheckExit = true;
    }
}
